package com.ls.blog.controller;


import static com.ls.blog.common.consts.Methods.*;

import com.ls.blog.aop.LogAop;
import com.ls.blog.dto.LikeDTO;
import com.ls.blog.dto.Recodes;
import com.ls.blog.dto.ArticleDTO;
import com.ls.blog.entity.Article;
import com.ls.blog.service.ArticleService;
import com.ls.blog.utils.R;
import com.ls.blog.utils.Result;
import com.ls.blog.vo.ArticleVO;
import com.ls.blog.vo.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lishun
 * @since 2022-01-14
 */
@RestController
@RequestMapping("/article")
@Api(tags = "文章模块")
public class ArticleController {

    @Autowired
    ArticleService articleService;




    @PostMapping("/admin/list")
    @ApiOperation(value = "后台博客列表分页查询",httpMethod = POST)
    @LogAop
    public Result<Recodes<ArticleDTO>> listArticleDTO(@RequestBody ArticleVO articleVO){
        return R.ok(articleService.getArticleListByCategoryId(articleVO));
    }

    /**
     * 根据博客标题查询
     * @param title
     * @return
     */
    @GetMapping("/getByTitle")
    @ApiOperation("通过文章标题查询")
    @ApiImplicitParam(name = "title",value = "文章的标题")
    public Result<List<Article>> findByTitle(String title){
        return R.ok(articleService.getByTitle(title));
    }



    @PostMapping("admin/saveOrUpdate")
    @ApiOperation(value = "保存或更新文章",httpMethod = POST)

    public Result<?> saveOrUpdateArticle(@RequestBody ArticleVO articleVO) {
        articleService.saveOrUpdateArticle(articleVO);
        return R.ok();
    }


    @DeleteMapping("admin/logicDelete")
    @ApiOperation(value = "逻辑删除文章",httpMethod = DELETE)
    public Result<?> logicDeleteArticle(String id){
        articleService.logicDeleteArticle(id);
        return R.ok();
    }


    @DeleteMapping("admin/delete")
    @ApiOperation(value = "回收站删除文章")
    public Result<?> deleteArticle(String id){
        articleService.deleteArticle(id);
        return R.ok();
    }


    @GetMapping("admin/recoveryArticle")
    @ApiOperation(value = "回收站还原文章",httpMethod = GET)
    public Result<?> recoveryBlog(String id){
        articleService.recoveryArticle(id);
        return R.ok();
    }


    @GetMapping("/common/getArticleById")
    @ApiOperation(value = "根据id查找博客",httpMethod = GET)
    public Result<ArticleDTO> getArticleById(String id){
        return R.ok(articleService.getArticleById(id));
    }


    @PostMapping("/getArticleByCategoryId")
    @ApiOperation(value = "根据分类id查找文章列表",httpMethod = POST)
    @Deprecated
    public Result<Recodes>  getBlogByTypeId(@RequestBody ArticleVO articleVO){
        return R.ok(articleService.getBlogByTypeId(articleVO));
    }

    //前台从这开始
    @PostMapping("/home/list")
    @ApiOperation(value = "前台获得文章列表",httpMethod = POST)
    public Result<Recodes<ArticleDTO>> getArticleList(@RequestBody PageVO pageVO){
        return R.ok(articleService.getArticleList(pageVO));
    }

    @PostMapping("/home/merge")
    @ApiOperation(value = "前台归档",httpMethod = POST)
    public Result<Recodes<Article>> merge(@RequestBody PageVO pageVO){
        return R.ok(articleService.merge(pageVO));
    }

    @GetMapping("home/articleUp")
    @ApiOperation(value = "文章点赞",httpMethod = GET)
    public Result<LikeDTO> articleUp(Integer id){
        return R.ok(articleService.articleUp(id));
    }

    @GetMapping("/home/news")
    @ApiOperation(value = "最新文章",httpMethod = GET)
    public Result<List<ArticleDTO>> news(){
        return R.ok(articleService.news());
    }

    @GetMapping("/home/hots")
    @ApiOperation(value = "焦点图",httpMethod = GET)
    public Result<List<Article>> hots(){
        return R.ok(articleService.hots());
    }

    @GetMapping("/admin/top/{id}")
    @ApiOperation(value = "置顶切换",httpMethod = GET)
    public Result<?> top(@PathVariable("id") Integer id){
        articleService.top(id);
        return R.ok();
    }





}

